
function load_datatable(url) {
    var table;
    //提示信息
    $.fn.dataTable.ext.errMode = 'none';
    var lang = {
        "bAutoWidth": false,
        "sProcessing": "处理中...",
        "sLengthMenu": "每页 _MENU_ 项",
        "sZeroRecords": "没有匹配结果",
        "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
        "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
        "sInfoPostFix": "",
        "sSearch": "搜索:",
        "sUrl": "",
        "sEmptyTable": "表中数据为空",
        "sLoadingRecords": "载入中...",
        "sInfoThousands": ",",
        "oPaginate": {
            "sFirst": "首页",
            "sPrevious": "上页",
            "sNext": "下页",
            "sLast": "末页",
            "sJump": "跳转"
        },
        "oAria": {
            "sSortAscending": ": 以升序排列此列",
            "sSortDescending": ": 以降序排列此列"
        }
    };
    //初始化表格
    table = $("#dataTable")
        .on('error.dt', function (e, settings, techNote, message) {
            console.warn(message)
        }).dataTable({
            destroy: true,
            autoWidth: false,
            language: lang, //提示信息
            stripeClasses: ["odd", "even"], //为奇偶行加上样式，兼容不支持CSS伪类的场合
            processing: true, //隐藏加载提示,自行处理
            serverSide: false, //启用服务器端分页
            searching: true, //禁用原生搜索
            orderMulti: true, //启用多列排序
            order: [], //取消默认排序查询,否则复选框一列会出现小箭头
            renderer: "bootstrap", //渲染样式：Bootstrap和jquery-ui
            pagingType: "simple_numbers", //分页样式：simple,simple_numbers,full,full_numbers
            columnDefs: [{
                "targets": 'nosort', //列的样式名
                "orderable": false //包含上样式名‘nosort'的禁止排序
            }],
            ajax: function (data, callback, settings) {
                //封装请求参数
                //console.log(param);
                //ajax请求数据
                $.ajax({
                    type: "GET",
                    beforeSend: function (request) {
                        request.setRequestHeader("uid", localStorage.getItem("token"));
                    },
                    url:url,
                    // url: "/admin/view-all?status=1",
                    cache: false, //禁用缓存
                    dataType: "json",
                    success: function (result) {
                        var returnData = {};
                        returnData.data = result.data;//返回的数据列表
                        callback(returnData);
                    }
                });
            },
            "columns": [
                {"data": "reportId", visible: false},
                {"data": "verifyId", visible: false},
                {"data": "reportNumber"},
                {"data": "checkDate"},
                {"data": "agentName"},
                {"data": "agentPhone"},
                {
                    "data": "status",
                    render: function (data, type, full) {
                        return "待审核";
                    }
                },
                {
                    render: function (data, type, full) {
                        var temp = JSON.stringify(full);
                        return "<button onclick='view_report_info(" + full.reportId + ");'  class='btn btn-info btn-icon-split'> <span class='text'>查看</span></button>" +
                            "<button onclick='verify_report_pass(" + temp + ");'  class='btn btn-success btn-icon-split'> <span class='text'>通过</span></button>" +
                            "<button onclick='verify_report_reject(" + temp + ")'  class='btn btn-danger btn-icon-split'> <span class='text'>驳回</span></button>";
                    },
                    // "sWidth": "10%"
                }
            ],
            "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {                    //列样式处理
            }
        })
        .api();
    //此处需调用api()方法,否则返回的是JQuery对象而不是DataTables的API对象
};

function verify_report_pass(temp) {
    if(confirm("确认通过吗？")){
        $.ajax({
            beforeSend: function (request) {
                request.setRequestHeader("uid", localStorage.getItem("token"));
            },
            url: "/admin/verify",
            type: "post",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({verifyId: temp.verifyId, status: 2, reportId: temp.reportId}),
            dateType: "json",
            success: function (data) {
                if (data.code == 200) {
                    $("#dataTable").DataTable().ajax.reload();
                }
                alert(data.message);
            }
        });
    }
}

function verify_report_reject(temp) {
    $("#write_error_modal").modal("show");
    $("#report_error_textarea").val("");
    $("#submit_error_info").click(function () {
        $.ajax({
            beforeSend: function (request) {
                request.setRequestHeader("uid", localStorage.getItem("token"));
            },
            url: "/admin/verify",
            type: "post",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({verifyId: temp.verifyId, status: 3, reportId: temp.reportId,message:$("#report_error_textarea").val().trim()}),
            dateType: "json",
            success: function (data) {
                if (data.code == 200) {
                    $("#dataTable").DataTable().ajax.reload();
                }
                alert(data.message);
            }
        });
    });
}
$(function () {
   load_datatable("/admin/view-all?status=1");
});
